package com.example.appforphysician;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.example.appforphysician.adapter.ServiceConnector;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

public class AddScheduleActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_schedule);

		Button btnCancel = (Button) findViewById(R.id.btnCancel);
		btnCancel.setOnClickListener(btnCancelClickListener);

		Button btnOK = (Button) findViewById(R.id.btnOK);
		btnOK.setOnClickListener(btnOKClickListener);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.add_schedule, menu);
		return true;
	}

	private View.OnClickListener btnOKClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {

			TextView tv_addsche_patientName = (TextView) findViewById(R.id.tv_addsche_patientName);
			TextView tv_addsche_patientService = (TextView) findViewById(R.id.tv_addsche_patientService);
			TextView tv_addsche_patientAddress = (TextView) findViewById(R.id.tv_addsche_patientAddress);

			DatePicker tv_addsche_patientDate = (DatePicker) findViewById(R.id.tv_addsche_patientDate);
			TimePicker tv_addsche_patientTime = (TimePicker) findViewById(R.id.tv_addsche_patientTime);

			Date date1 = (Date) new Date(tv_addsche_patientDate.getYear(),
					tv_addsche_patientDate.getMonth(),
					tv_addsche_patientDate.getDayOfMonth());
			SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
			String dateString = sdf.format(date1);

			String formattedTime = "";
			int hour = tv_addsche_patientTime.getCurrentHour();
			String sHour = "00";
			String sign = "AM";
			if (hour < 10) {
				sHour = "0" + hour;
			} else {			
				
				if( hour <= 12 )
				{
					sHour = String.valueOf(hour);
				}
				else{
					sHour = String.valueOf(hour-12);
					sign = "PM";
				}
			}

			int minute = tv_addsche_patientTime.getCurrentMinute();
			String sMinute = "00";
			if (minute < 10) {
				sMinute = "0" + minute;
			} else {
				sMinute = String.valueOf(minute);
			}

			formattedTime = sHour + ":" + sMinute + " " + sign;

			ServiceConnector
					.getSingleton()
					.getPhysicianSchedule()
					.addSchedule(tv_addsche_patientName.getText().toString(),
							tv_addsche_patientService.getText().toString(),
							tv_addsche_patientAddress.getText().toString(),
							dateString, formattedTime);

			// -----------------Change Screen-------------
			Intent intent = new Intent(getApplicationContext(),
					ManagerActivity.class);
			startActivity(intent);
		}
	};

	private View.OnClickListener btnCancelClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			// -----------------Change Screen-------------
			Intent intent = new Intent(getApplicationContext(),
					ManagerActivity.class);
			startActivity(intent);
		}
	};

}
